//
//  UIViewController+SGExtension.swift
//  QingLiuSwift
//
//  Created by xyanl on 2024/11/12.
//  Copyright © 2024 考拉🐨. All rights reserved.
//

import UIKit

extension UIViewController: SpadgerCompatible {}

public extension Spadger where Base: UIViewController {
    
    /// 查找目标控制器
    /// - Parameters:
    ///   - from: 根控制器
    ///   - target: 需要查找的目标控制器
    /// - Returns: 找到的目标控制器
    static func findTargetViewController(from: UIViewController = UIWindow.sg.keyWindow.rootViewController!, _ target: AnyClass) -> UIViewController? {
        // 目标控制器
        var targetVC: UIViewController?
        var rootVC = from
        
        // 当VCA模态的弹出了VCB，那么VCA就是 presentingViewController，VCB就是 presentedViewController。
        if(rootVC.presentedViewController != nil) {
            rootVC = rootVC.presentedViewController!
        }
        
        if rootVC.isKind(of: UITabBarController.self) {
            // 根视图为 UITabBarController
            targetVC = findTargetViewController(from: ((rootVC as! UITabBarController).selectedViewController)!, target)
            
        } else if rootVC.isKind(of: UINavigationController.self) {
            // 根视图为 UINavigationController
            rootVC.children.forEach { viewController in
                if viewController.isKind(of: target.self) {
                    targetVC = viewController
                }
            }
            
        } else {
            targetVC = rootVC
        }
        return targetVC
    }
    
    // 获取当前显示的控制器
    static var currentViewController: UIViewController? {
        // 从 UIWindow 获取根控制器
        guard let rootViewController = UIWindow.sg.keyWindow.rootViewController else {
            return nil // 如果根控制器为空，返回 nil
        }
        return getVisibleViewController(from: rootViewController)
    }
    
    private static func getVisibleViewController(from vc: UIViewController) -> UIViewController? {
        if let navigationController = vc as? UINavigationController {
            // 如果是导航控制器，则返回最上层的控制器
            return getVisibleViewController(from: navigationController.visibleViewController!)
        }
        
        if let tabBarController = vc as? UITabBarController {
            // 如果是标签栏控制器，则返回选中的控制器
            if let selected = tabBarController.selectedViewController {
                return getVisibleViewController(from: selected)
            }
        }
        
        if let presented = vc.presentedViewController {
            // 如果有呈现的控制器，则递归查找
            return getVisibleViewController(from: presented)
        }
        // 返回当前控制器
        return vc
    }
}


extension UIViewController {
    func push(_ viewController: UIViewController, animated: Bool = true) {
        navigationController?.pushViewController(viewController, animated: animated)
    }
    
    func present(_ viewController: UIViewController, style: UIModalPresentationStyle = UIModalPresentationStyle.fullScreen, completion: (() -> Void)? = nil) {
        viewController.modalPresentationStyle = style
        present(viewController, animated: true, completion: completion)
    }
    
    @objc func back(_ animated: Bool = true) {
        if let viewControllers = navigationController?.viewControllers {
            if viewControllers.count > 1 {
                if viewControllers[viewControllers.count - 1] == self {
                    // push方式
                    navigationController?.popViewController(animated: animated)
                }
            } else {
                dismiss(animated: animated)
            }
        }
    }
    
}


extension UIViewController {
    /// 隐藏 tabbar 动画
    func hiddenTabBar(_ hidden: Bool, animated: Bool = true, completion: (() -> Void)? = nil) {
//        guard let tabBar = self.tabBarController?.tabBar else { return }
//        if tabBar.isHidden == hidden { return }
//        let screenH = UIScreen.main.bounds.height
//        xLog("screenH \(screenH)")
//        let frame = tabBar.frame
//        let y = hidden ? screenH : screenH - frame.size.height
//        let duration: TimeInterval = (animated ? 0.25 : 0.0)
//        
//        if tabBar.isHidden {
//            // 有的时候, 会出现 tabBar.frame 回到了现实的位置, 隐藏的时候移出屏幕外
//            tabBar.frame = CGRect(x: 0, y: screenH, width: frame.size.width, height: frame.size.height)
//            tabBar.layoutIfNeeded()
//        }
//        
//         xLog("tabbar 改变前 frame \(tabBar.frame)")
//         xLog("tabbar 改变前 isHidden \(tabBar.isHidden)")
//        
//        // 先显示出来, 然后做动画操作
//        tabBar.isHidden = false
//        
//        UIView.animate(withDuration: duration, animations: {
//            tabBar.frame = CGRect(x: 0, y: y, width: frame.size.width, height: frame.size.height)
//        }) { finish in
//            tabBar.isHidden = hidden
//             xLog("tabbar 改变后 frame \(tabBar.frame)")
//             xLog("tabbar 改变后 \(tabBar.isHidden)")
//            completion?()
//        }
    }
}
